#!/usr/bin/env bash

# Determine OS
UNAME=`uname`;
if [[ $UNAME == "Darwin" ]];
then
    OS="macos";
else
    if [[ $UNAME == "Linux" ]];
    then
        UBUNTU_RELEASE=`lsb_release -a 2>/dev/null`;
        if [[ $UBUNTU_RELEASE == *"16.04"* ]];
        then
            OS="ubuntu1604";
        else
            OS="ubuntu1404";
        fi
    else
        echo "Unsupported Operating System: $UNAME";
    fi
fi
echo "🖥 Operating System: $OS";

if [[ $OS != "macos" ]];
then
    echo "🐦 Installing Swift";
    eval "$(curl -sL https://apt.vapor.sh)";
    sudo apt-get install vapor;
    sudo chmod -R a+rx /usr/;
fi

echo "📅 Version: `swift --version`";

echo "🚀 Building";
swift build
if [[ $? != 0 ]];
then
    echo "❌  Build failed";
    exit 1;
fi

echo "💼 Building Release";
swift build -c release
if [[ $? != 0 ]];
then
    echo "❌  Build for release failed";
    exit 1;
fi

echo "🔎 Testing";

swift test
if [[ $? != 0 ]];
then
    echo "❌ Tests failed";
    exit 1;
fi

if [[ $OS == "macos" ]];
then
    echo "Running on $OS - building on Xcode"
    xcodebuild -project SwiftSoup.xcodeproj -scheme SwiftSoup -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO -destination 'platform=iOS Simulator,name=iPhone 6' build test
    if [[ $? != 0 ]];
    then
        echo "❌ Xcode Project Tests failed";
        exit 1;
    fi

    pod lib lint
    if [[ $? != 0 ]];
    then
        echo "❌ Pod failed to lint";
        exit 1;
    fi
fi

echo "✅ Done"
